﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmPatrolException :FrmBase
    {
        private PatrolItem patrolItem;
        private PlanDevice planDevice;
        private PatrolDetail patrolDetail;
        private int updateState;  //更新状态 0:新增 1:更新
        private PatrolInfoService patrolInfoService = new PatrolInfoService();

        public FrmPatrolException()
        {
            InitializeComponent();
            ttlbrhead.Text = ResourceImage.巡视管理;
            ilstbcup.Images.Add(ResourceImage.确定01);
            ilstbcup.Images.Add(ResourceImage.键盘01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.确定02);
            ilstdown.Images.Add(ResourceImage.键盘02);
            ilstdown.Images.Add(ResourceImage.返回02);
        }

        private void FrmPatrolException_Load(object sender, EventArgs e)
        {
            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbPatrolItemName.Text = patrolItem.ItemName;
            updateState = patrolDetail == null ? 0 : 1;

            if (patrolDetail != null)
                tbException.Text = patrolDetail.Content;
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            patrolItem = (PatrolItem)Parameter[0];
            planDevice = (PlanDevice)Parameter[1];
            patrolDetail = (PatrolDetail)Parameter[2];
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            patrolItem = (PatrolItem)Caches[0];
            planDevice = (PlanDevice)Caches[1];
            patrolDetail = (PatrolDetail)Caches[2];
        }



        private void bcxunjian_OnButtonClick_1(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0:
                    if (SavePatrolDetail())
                        mssdlgexec.ShowDialog(this, "保存完成", DialogType.Confirm, FormManage.form_Deactivate);
                    break;
                case 1:
                    InputMethod.SHFullScreen(this.Handle, 4);
                    InputMethod.SipShowIM(1);
                    tbException.Focus();
                    break;
                case 2:
                    FormManage.GotoNext("FrmPatrolPoint", null,null,true);
                    break;
            }
        }

        private bool SavePatrolDetail()
        {
            try
            {
                if (updateState == 0)
                {
                    //新增
                    patrolDetail = new PatrolDetail();
                    patrolDetail.PatrolDetailID = Guid.NewGuid().ToString();
                    patrolDetail.PlanID = planDevice.PlanID;
                    patrolDetail.Content = tbException.Text;
                    patrolInfoService.AddProtolDetail(patrolDetail, planDevice, patrolItem);
                    updateState = 1;
                }
                else
                {
                    //更新
                    patrolDetail.Content = tbException.Text;
                    patrolInfoService.UpdatePatrolDetail(patrolDetail);
                }

                return true;
            }
            catch (Exception e)
            {

                return false;
            }

        }

        private void btnPhoto_Click(object sender, EventArgs e)
        {
            //如果没有保存巡视详情，线保存巡视详情
            SavePatrolDetail();

            if (patrolDetail == null)
                return;

            LinkFileService linkFileService = new LinkFileService();
            LinkFile linkFile = linkFileService.TaskPicture(patrolDetail.PatrolDetailID);
            if (linkFile == null)
                return;

            FormManage.GotoNext("FrmCamera", new object[] { "FrmPatrolException", linkFile }, new object[] { patrolItem, planDevice, patrolDetail }, false);


            ////获取改异常关联的图片个数，如果没有，则启动拍照
            //LinkFileService linkFileService = new LinkFileService();
            //List<LinkFile> linkFiles = linkFileService.GetLinkFileByLinkID("'" + patrolDetail.PatrolDetailID + "'");

            //if (linkFiles.Count == 0)
            //{
            //    LinkFile linkFile = linkFileService.TaskPicture(patrolDetail.PatrolDetailID);
            //    FormManage.GotoNext("FrmCamera", new object[] { "FrmPatrolException",linkFile }, new object[] { patrolItem, planDevice, patrolDetail }, false);
            //}
            //else
            //{
            //    //直接显示图片列表
            //    FormManage.GotoNext("FrmPictureList", new object[] { "FrmPatrolException","'" + patrolDetail.PatrolDetailID +"'"}, new object[] { patrolItem, planDevice, patrolDetail }, false);

            //}

        }

        private void btnAudio_Click(object sender, EventArgs e)
        {
            //如果没有保存巡视详情，线保存巡视详情
            SavePatrolDetail();

            if (patrolDetail == null)
                return;

            FormManage.GotoNext("FrmRecording", new object[] { "FrmPatrolException", patrolDetail.PatrolDetailID }, new object[] { patrolItem, planDevice, patrolDetail }, false);

        }


    }
}